//
//  DSSHPhotoMainVC.m
//  DSSH
//
//  Created by lcy on 16/3/28.
//  Copyright © 2016年 lcy. All rights reserved.
//

#import "DSSHDetailinfoVC.h"
#import "CusAlterview.h"
#import "DSSHphotoS.h"
#import "UIView+Ext.h"


#define LINE 0.5
#define BUTTONWIDTH (([UIScreen mainScreen].bounds.size.width -(titlearray.count )*2)/titlearray.count-1)
#define TABLEWIDTH ([UIScreen mainScreen].bounds.size.width - 20)
#define PHOTOW ([UIScreen mainScreen].bounds.size.width - 20)/2
#define PHOTOH 200

@interface DSSHDetailinfoVC ()<DSSHphotoSdelegate,UIScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    UIView *_modleview;
    UIView *_scview;
    UIScrollView *_dataviews;
    NSArray *_dataarray;
    int _index;
    NSMutableArray * _framearay;
    UIButton *dropbutton;
    DSSHphotoS *_Beselect;
    
}
@property(nonatomic,strong)UICollectionView *tableView0;
@property(nonatomic,strong)UICollectionView *tableView1;
@property(nonatomic,strong)UICollectionView *tableView2;

@end

@implementation DSSHDetailinfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataarray = @[@"1",@"2"];
    [self setbasicview];
    //self.view.frame = CGRectMake(0, 64,ZYAppWidth,ZYAppHeight);
    _dataviews = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 110, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    _dataviews.delegate = self;
    _dataviews.contentSize =CGSizeMake(ZYAppWidth * 3, 0);
    [_dataviews setBackgroundColor:[UIColor colorWithRed:246/255.0 green:246/255.0 blue:246/255.0 alpha:1.0]];
   _dataviews.pagingEnabled = YES;
    //设置左边按钮显示的内容
    UIButton *leftbutton = [[UIButton alloc] init];
    [leftbutton setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    [leftbutton addTarget:self action:@selector(Hitleftbutton) forControlEvents:UIControlEventTouchUpInside];
    leftbutton.frame = CGRectMake(0, 0, 30, 30);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftbutton];
    //设置titleview的标题
    self.navigationItem.title = self.titlestr;
    [self.view addSubview:_dataviews];
    
    
    //数据
    _dataoftable = [NSMutableArray array];
    for (int i = 0 ; i < 3; i ++ ) {
        NSMutableArray *tmpnarray = [NSMutableArray array];
        
        for (int k =0; k < (i+1) *3; k++) {
            
            [tmpnarray addObject:[NSString stringWithFormat:@"%d",k]];
        }
        [self.dataoftable addObject:tmpnarray];
    }
    
    [self initUI];

}
-(void)Hitleftbutton{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)initUI{
    int col  = 2;
    
    _framearay = [NSMutableArray array];
    for (int i = 0; i < _dataarray.count; i++ ) {
        int rowP = (int)((NSArray *)_dataoftable[i]).count / col +1;
        UICollectionView  * _scrollV = [self createCollectionView];
        _scrollV.backgroundColor = [UIColor whiteColor];
        _scrollV.delegate = self;
        _scrollV.dataSource = self;
        _scrollV.tag = 10000+i;
        _scrollV.layer.cornerRadius = 7.0;
        [_dataviews addSubview:_scrollV];
        [_scrollV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_dataviews.mas_left).with.offset(10*(i+1) + i*(TABLEWIDTH +10));
            make.width.mas_equalTo(TABLEWIDTH);
            if((PHOTOH)*rowP < ZYAppHeight -44 -64 -5)
            {
                make.height.mas_equalTo((PHOTOH)*rowP);

            }
            else
            make.height.mas_equalTo(ZYAppHeight -44-64 -10);
            make.top.mas_equalTo(5);
        }];
      }
    
    
    self.tableView0 = [_dataviews viewWithTag:10000];
    self.tableView1 = [_dataviews viewWithTag:10001];
    self.tableView2 = [_dataviews viewWithTag:10002];

}

- (UICollectionView *)createCollectionView{
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        UICollectionView * collectionViewLunBo = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, 320, 200) collectionViewLayout:layout];
        collectionViewLunBo.dataSource = self;
        collectionViewLunBo.delegate = self;
        collectionViewLunBo.backgroundColor = [UIColor yellowColor];
    
        //注册Cell类，否则崩溃: must register a nib or a class for the identifier or connect a prototype cell in a storyboard
//        [collectionViewLunBo registerClass:[DSSHphotoS class] forCellWithReuseIdentifier:@"DSSHphotoS"];
    
    UINib *nib = [UINib nibWithNibName:@"DSSHphotoS" bundle:nil];
    [collectionViewLunBo registerNib:nib forCellWithReuseIdentifier:@"DSSHphotoS"];
    
        [_dataviews addSubview:collectionViewLunBo];
        return collectionViewLunBo;
}

//滑动时判断滚动条应该的位置
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //     scrollView的contentOffset是最重要的属性,点,x,y记录的是滚动的距离,相对的是scrollView的可视界面的左上角的距离
    
    NSInteger page = _dataviews.contentOffset.x / ZYAppWidth ;
    
    [self viewsrcoll:[_modleview viewWithTag:page + 10]];
    
    _index = (int)page;
    
}


-(void)viewsrcoll:(UIButton *)button{
    //移动滑动条
    [UIView animateWithDuration:0.25 animations:^{
        CGPoint center = CGPointMake(button.center.x, _scview.center.y);
        _scview.center =center;
    }];
    if(dropbutton !=nil)
    {
        [dropbutton removeFromSuperview];
        dropbutton = nil;
    }
    [_Beselect.SelectFlag setHidden:YES];
    
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    if (scrollView.contentOffset.x > 0) {
        
    }
    
    if ([scrollView isEqual:_dataviews]) {
        
    }
}

-(void)setbasicview
{
    UIView *modeview1= [[UIView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 44)];
    modeview1.backgroundColor = [UIColor whiteColor];
    NSArray *titlearray = @[@"上刊模式",@"巡查模式",@"检修模式"];
    _dataarray = titlearray;
    for (int i = 0; i < titlearray.count; i++) {
        UIButton *button = [[UIButton alloc] init];
        [button setTitle:titlearray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(selectModle:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 10+i;
        [modeview1 addSubview:button];
        button.titleLabel.font = [UIFont systemFontOfSize:12.0];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(BUTTONWIDTH);
            make.left.equalTo(modeview1.mas_left).with.offset(i*BUTTONWIDTH+(i+1)*LINE);
            make.top.equalTo(modeview1.mas_top).with.offset(0);
            make.height.equalTo(@44);
            
        }];
        if(i == _dataarray.count -1){
            [self selectModle:button];
        }
        
        
    }
    for (int i = 0; i < titlearray.count -1; i++) {
        UIView *blackview = [[UIView alloc]init];
        blackview.backgroundColor = [UIColor grayColor];
        [modeview1 addSubview:blackview];
        [blackview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(modeview1.mas_left).with.offset((BUTTONWIDTH+LINE)*(i+1));
            make.centerY.equalTo(modeview1.mas_centerY);
            make.width.mas_equalTo(LINE);
            make.height.equalTo(@38);
        }];
        
    }
    //添加滑动条
    UIView *scview = [[UIView alloc]init];
    scview.backgroundColor = [UIColor redColor];
    [modeview1 addSubview:scview];
    [scview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@3);
        make.bottom.equalTo(modeview1.mas_bottom).with.offset(0);
        make.centerX.equalTo([modeview1 viewWithTag:10]);
        make.width.mas_equalTo(BUTTONWIDTH - 20);
    }];
    _scview = scview;
    _modleview = modeview1;
    
    [self.view addSubview:_modleview];
    _index = 0;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 事件点击按钮
-(IBAction)droppress:(UIButton *)button{
    
    
    
    UICollectionView * coll =[_dataviews viewWithTag:[[NSString stringWithFormat:@"1000%d",_index] intValue]];
    
    NSIndexPath *indexpath = [coll indexPathForCell:_Beselect];
    

    [_dataoftable[_index] removeObjectAtIndex:indexpath.item];
    
    [coll deleteItemsAtIndexPaths:@[indexpath]];
        [coll reloadData];
    unsigned long count = ((NSArray *)_dataoftable[_index]).count+1;
    if( count %2 && coll.contentSize.height -PHOTOH<= coll.h)
    {
        coll.h = coll.contentSize.height - PHOTOH;
    }
    [UIView animateWithDuration:0.5 animations:^{
        dropbutton.frame = CGRectMake(0, ZYAppHeight, ZYAppWidth, 0);
    } completion:^(BOOL finished) {
        [dropbutton removeFromSuperview];
        dropbutton = nil;
    }];
    if(((NSArray *)_dataoftable[_index]).count == 0)
    {
        coll.h = 0;
        coll.w = 0;
    }
}
-(void)selectModle:(UIButton *)button
{
    DSSHLog(@"modle");
    
    //移动滑动条
    [UIView animateWithDuration:0.5 animations:^{
        CGPoint center = CGPointMake(button.center.x, _scview.center.y);
        _scview.center =center;
    }];
    if(dropbutton !=nil)
    {
        [dropbutton removeFromSuperview];
        dropbutton = nil;
    }
    [_Beselect.SelectFlag setSelected:NO];
    _index = (int)button.tag - 10 ;
    [UIView animateWithDuration:0.5 animations:^{
        _dataviews.contentOffset= CGPointMake((self.view.bounds.size.width ) * _index  , 0);
    }];
    
}
#pragma mark 代理事件
-(void)imagebuttonpresslong:(DSSHphotoS *)photos
{
    if(dropbutton == nil)
    {
        dropbutton = [[UIButton alloc] init];
        [dropbutton setBackgroundColor:[UIColor redColor]];
        [dropbutton setTitle:@"删除" forState:UIControlStateNormal];
        [dropbutton addTarget:self action:@selector(droppress:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:dropbutton];
        [dropbutton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.view.mas_bottom).with.offset(0);
            make.left.equalTo(self.view.mas_left).with.offset(0);
            make.right.equalTo(self.view.mas_right).with.offset(0);
            make.height.equalTo(@45);
        }];
    }
    _Beselect = photos;
}

#pragma mark - Collection的代理
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if(collectionView == _tableView0)
    {
        return ((NSMutableArray *)_dataoftable[0]).count;
    }
    else if(collectionView == _tableView1)
    {
        return ((NSMutableArray *)_dataoftable[1]).count;
    }
    else if(collectionView == _tableView2)
    {
        return ((NSMutableArray *)_dataoftable[2]).count;
    }
    else return 0;
}


//-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
//{
//    return UIEdgeInsetsMake(10, 10, 10, 10);
//}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger falg = 0 ;
    if(collectionView == _tableView0)
    {
       falg= 0;
    }
    else if(collectionView == _tableView1)
    {
        falg= 1;
    }
    else if(collectionView == _tableView2)
    {
        falg= 2;
    }
    DSSHphotoS *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DSSHphotoS" forIndexPath:indexPath ];
    cell.DetailT.text = _dataoftable[falg][indexPath.item];
    cell.delegate = self;
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(collectionView.w/2 ,  PHOTOH);
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
-(NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
@end

